package com.module.product.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;

import com.module.product.vo.AttrItem;
import com.module.product.vo.EntAttrValue;


public class EntAttrValueDao extends GenericHibernateDao<EntAttrValue, String> {
    private static  EntAttrValueDao instance = new EntAttrValueDao();

    private EntAttrValueDao() {
    }

    public synchronized static EntAttrValueDao getInstance() {
        return instance;
    }
    public boolean delAllAttrVbyPid(Connection conn,String productId) {
        boolean b = false;
        PreparedStatement stmt = null;
        try
        {
        	//String sql = " delete from  p_ent_attr_value  where  c_product_id =? and c_attrgroup_id =? ";
            String sql = " delete from  p_ent_attr_value  where  c_product_id =?  ";
            stmt = conn.prepareStatement(sql);
                              stmt.setString(1,productId);
            stmt.executeUpdate();
            b = true ;
        }catch(Exception e){
        	e.printStackTrace();
        }finally{
        	this.close(stmt);
        }
        return b;
    }
    public List<AttrItem> listByGroupId(Connection conn,String attrGroupId,String productId) throws SQLException{
        List<AttrItem> list  = new ArrayList<AttrItem>();
        AttrItem attr = null;
        PreparedStatement stmt = null;
        try
        {
        String sql = "select a.c_id,a.c_name,b.c_attr_value ,a.c_groupId,a.c_desc ," +
        		"b.c_product_id,a.c_type,b.i_attr_value_size,b.c_original_value from p_attr_item a left join p_ent_attr_value b" +
        		" on a.c_id = b.c_attritem_id  and b.c_product_id =? where a.c_status !=? and a.c_groupId in (select c_id from p_attr_group where c_category_id = ? ) ";
        stmt = conn.prepareStatement(sql);
                          stmt.setString(1,productId);
                          stmt.setString(2,CUDEnum.D.toString());
                          stmt.setString(3,attrGroupId);
        ResultSet rs  = stmt.executeQuery();  
        while(rs.next()){ 
            attr = new AttrItem();
            attr.setId(rs.getString(1));
            attr.setName(rs.getString(2));
            attr.setAttrValue(rs.getString(3));
            attr.setAttrGroup(rs.getString(4));
            attr.setDesc(rs.getString(5));
            attr.setType(rs.getString(7));
            attr.setAttrValueSize(rs.getInt(8));
            attr.setOriginalValue(rs.getString(9));
            list.add(attr);
           }                  
        }finally{
        	close(stmt);
        }
        return list;
    }
    @SuppressWarnings("unchecked")
	public List<EntAttrValue> getEntAttvByProductid(Session session,String productId){
        List<EntAttrValue> list = null;
        String hql = "from EntAttrValue where  productId=?";
        Query query = session.createQuery(hql);
        query.setString(0, productId);
        list = query.list();
        return list;
    }
}
